package org.springframework.boot;

import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.ResourcePropertySource;

import java.io.IOException;

/**
 * @author shiminghui
 * @date 2025/5/8 15:58
 * @description: TODO
 */
public class Step6 {
    public static void main(String[] args) throws IOException {
//        test();
        SpringApplication application = new SpringApplication();

        // 第六步其实就是将配置文件中以spring.main打头的属性加载到springApplication的属性中
        ApplicationEnvironment environment = new ApplicationEnvironment();
        environment.getPropertySources().addLast(new ResourcePropertySource(new ClassPathResource("step6.properties")));

        System.out.println(application);

        Binder.get(environment).bind("spring.main", Bindable.ofInstance(application));
        // 测试
        System.out.println(application);

    }

    public static void test() throws IOException {

        ApplicationEnvironment environment = new ApplicationEnvironment();
        environment.getPropertySources().addLast(new ResourcePropertySource(new ClassPathResource("step4.properties")));

        BindResult<Test> test = Binder.get(environment).bind("test", Test.class);
        System.out.println(test.get());

    }

    // 这里的属性注入是用的getter和setter方法,如果没有的话会导致注入失败
    static class Test {
        private String xiaoMing;
        private String xiaoWang;
        private String xiaoLi;

        public String getXiaoMing() {
            return xiaoMing;
        }

        public void setXiaoMing(String xiaoMing) {
            this.xiaoMing = xiaoMing;
        }

        public String getXiaoWang() {
            return xiaoWang;
        }

        public void setXiaoWang(String xiaoWang) {
            this.xiaoWang = xiaoWang;
        }

        public String getXiaoLi() {
            return xiaoLi;
        }

        public void setXiaoLi(String xiaoLi) {
            this.xiaoLi = xiaoLi;
        }

        @Override
        public String toString() {
            return "Test{" +
                    "xiaoMing='" + xiaoMing + '\'' +
                    ", xiaoWang='" + xiaoWang + '\'' +
                    ", xiaoLi='" + xiaoLi + '\'' +
                    '}';
        }
    }
}
